//
//  CXIngredientsViewController.m
//  Delicious
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "CXIngredientsViewController.h"
#import "CXIngredientCollectionViewCell.h"
#import "CXNetHandle.h"
#import "CXIngreModel.h"

@interface CXIngredientsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) UIView *bottomView;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIImageView *image;


@end

@implementation CXIngredientsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(80,80);
    //上下滚动
    layout.scrollDirection = UICollectionViewScrollPositionCenteredVertically;
    layout.sectionInset  = UIEdgeInsetsMake(100, 30, 30, 30);
//    layout.minimumInteritemSpacing = 20;
    //行间距
    layout.minimumLineSpacing =50;
    //创建collectonView
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
//    self.collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[CXIngredientCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [self getData];
    
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 100)];
    self.bottomView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.bottomView];
    
    
    self.image = [[UIImageView alloc] initWithFrame:CGRectMake(160, 40, 30, 40)];
    self.image.backgroundColor = [UIColor redColor];
    [self.image setImage:[UIImage imageNamed: @"iconfont-xiangxia.png"]];
    [self.bottomView addSubview:self.image];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, 250, 30)];
    self.label.text = @"先选一个最好吃的食材!";
    self.label.font = [UIFont systemFontOfSize:20];
    self.label.backgroundColor = [UIColor purpleColor];
    [self.bottomView addSubview:self.label];

    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 100, 100)];
    self.scrollView.backgroundColor = [UIColor redColor];
    self.scrollView.alpha = 0;
    [self.bottomView addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(375 * 2, 100);
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(self.scrollView.frame.size.width + 10 , 10, 80, 80);
    self.button.backgroundColor = [UIColor orangeColor];
    self.button.alpha = 0;
    [self.button setTitle:@"可作菜品" forState:UIControlStateNormal];
    self.button.layer.cornerRadius = 40;
    [self.bottomView addSubview:self.button];
    [self.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
}

#pragma mark -- 获取数据
-(void)getData
{
    [CXNetHandle getUrl:@"http://api.wecook.cn/v3/GuideSearch/ingredients?wid=ac33e587d327b60019bb131b70f719ed&sign=9d6724507a5897ced464c344984b3884&keys=&timestamp=1448594002149&net=wifi&access=259eea423dee18c7b865b0777cd657cc&type=&batch=30&page=1" cookie:nil result:^(id result) {
        NSDictionary *dic = result;
        NSArray *arr = dic[@"result"][@"ing"];
        self.dataArr = [CXIngreModel transformWithArray:arr];
        [self.collectionView reloadData];
        
        
    }];
}




#pragma mark 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CXIngredientCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    cell.message = self.dataArr[indexPath.item];
    return cell;
}
#pragma amrk -- 点击item
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    self.scrollView.alpha = 1;
    self.button.alpha = 1;
    self.label.alpha = 0;


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
